package com.toycloud.business.controller.feedback;

import com.toycloud.business.dto.params.FeedbackAnalysisParams;
import com.toycloud.business.dto.params.FeedbackRecordParams;
import com.toycloud.business.dto.params.SaveFeedbackRecordParams;
import com.toycloud.business.dto.results.*;
import com.toycloud.business.service.IDemandRecordService;
import com.toycloud.business.service.IUserFeedBackService;
import com.toycloud.common.core.utils.poi.RuoyiExcelUtil;
import com.toycloud.system.utils.OnesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author xqyang5
 * @Date 2024/9/18 17:16
 */
@RestController
@RequestMapping("/userFeedBack")
public class UserFeedBackController {
    @Autowired
    private IUserFeedBackService userFeedBackService;
    @Autowired
    private IDemandRecordService iDemandRecordService;

    @PostMapping("/list")
    public CommonResult<List<FeedbackRecordResult>> list(@Valid @RequestBody FeedbackRecordParams params) {
        return userFeedBackService.getFeedbackRecordList(params);
    }

    @PostMapping("/export")
    public void export(@RequestBody FeedbackRecordParams params, HttpServletResponse response) {
        List<FeedbackRecordResultVo> list = userFeedBackService.getFeedbackRecordVO(params);
        RuoyiExcelUtil.exportExcel(list, "反馈记录", FeedbackRecordResultVo.class, response);

    }

    @PostMapping("/getFeedbackStatistics")
    public CommonResult<FeedbackAnalysisResult> getFeedbackStatistics(@Valid @RequestBody FeedbackAnalysisParams params) {
        return userFeedBackService.getFeedbackStatistics(params);
    }

    @PostMapping("/exportFeedbackStatistics")
    public CommonResult<String> exportFeedbackStatistics(@Valid @RequestBody FeedbackAnalysisParams params) {
        return userFeedBackService.exportFeedbackStatistics(params);
    }

    @GetMapping("/getTaskStatuses")
    public CommonResult<List<OnesUtil.StatuDetail>> getTaskStatuses(){
        return userFeedBackService.getTaskStatuses();
    }

    @PostMapping("/multiUpdateFeedbackStatistics")
    public CommonResult<Boolean> multiUpdateFeedbackStatistics() {
        return userFeedBackService.multiUpdateFeedbackStatistics();
    }

    @PostMapping("/save")
    public CommonResult<Boolean> save(HttpServletRequest request,@Valid @RequestBody SaveFeedbackRecordParams param) {
//        UserParam userParam =iDemandRecordService.username(request);
//        param.setRecorder(userParam.getUsername());
        return userFeedBackService.saveFeedbackRecord(param);
    }

    @GetMapping("/delete")
    public CommonResult<Boolean> delete(long id) {
        return userFeedBackService.deleteFeedbackRecord(id);
    }

    @PostMapping("/multiUploadFile")
    public CommonResult<String> uploadFiles(@RequestParam("files") MultipartFile[] files) {
        if (files.length > 5) {
            return  CommonResult.Fail("最多只能上传5个文件");
        }

        return userFeedBackService.multiUploadFile(files);
    }

    @PostMapping("/importFeedbackData")
    public CommonResult<Boolean> importFeedbackData(@RequestParam("file") MultipartFile file,@RequestParam("username") String username) {
        return userFeedBackService.importFeedbackData(file,username);
    }

}
